package net.zoneland.knowledge.dao;

import net.zoneland.knowledge.model.ScheduledTask;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * 定时任务通知告警配置数据接口.
 */
@Mapper
public interface ScheduledTaskMapper {

    /**
     * 根据id更新定时任务表达式.
     *
     * @param scheduledTask 定时任务
     * @return 更新结果
     */
    @Update({
        "update AITK_SCHEDULED_TASK",
        "set CRON = #{cron, jdbcType=VARCHAR}",
        "where ID = #{id, jdbcType=VARCHAR}"
    })
    int updateCronExpressionByCronKey(final ScheduledTask scheduledTask);

    /**
     * 根据id更新定时任务状态.
     *
     * @param scheduledTask 定时任务
     * @return 更新结果
     */
    @Update({
        "update AITK_SCHEDULED_TASK",
        "set STATE = #{state, jdbcType=INTEGER}",
        "where ID = #{id, jdbcType=VARCHAR}"
    })
    int updateStatusByCronKey(final ScheduledTask scheduledTask);

    /**
     * 查询所有定时任务信息对象.
     *
     * @return 定时任务
     */
    @Select({
        "select ID, CLASSIFICATION, TASK_NAME, TASK_DESCRIPTION, CLASS_NAME, METHOD_NAME, ",
        "CRON, STAFFING, NOTIFICATION_CONTENT, NOTIFICATION_MODE, STATE, ",
        "CREATE_BY, CREATE_TIME, UPDATE_BY, UPDATE_TIME ",
        "FROM aitk_scheduled_task "
    })
    @ResultMap(value = {"ScheduledTaskResultMap"})
    List<ScheduledTask> selectAll();

    /**
     * 查询所有启用定时任务信息对象.
     *
     * @return 定时任务
     */
    @Select({
        "select ID, CLASSIFICATION, TASK_NAME, TASK_DESCRIPTION, CLASS_NAME, METHOD_NAME, ",
        "CRON, STAFFING, NOTIFICATION_CONTENT, NOTIFICATION_MODE, STATE, ",
        "CREATE_BY, CREATE_TIME, UPDATE_BY, UPDATE_TIME ",
        "FROM aitk_scheduled_task ",
        "WHERE STATE = #{state, jdbcType=INTEGER}"
    })
    @ResultMap(value = {"ScheduledTaskResultMap"})
    List<ScheduledTask> selectByTaskStatus(final Integer state);

    @Delete({
        "delete from AITK_SCHEDULED_TASK",
        "where ID = #{id, jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(final String id);

    /**
     * 通过id查询定时任务对象.
     *
     * @param id 定时任务id
     * @return 定时任务
     */
    @Select({
        "select ID, CLASSIFICATION, TASK_NAME, TASK_DESCRIPTION, CLASS_NAME, METHOD_NAME, ",
        "CRON, STAFFING, NOTIFICATION_CONTENT, NOTIFICATION_MODE, STATE, ",
        "CREATE_BY, CREATE_TIME, UPDATE_BY, UPDATE_TIME ",
        "FROM aitk_scheduled_task ",
        "WHERE ID = #{id, jdbcType=VARCHAR}"
    })
    @Results(id = "ScheduledTaskResultMap", value = {
        @Result(column = "ID", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
        @Result(column = "CLASSIFICATION", property = "classification", jdbcType = JdbcType.VARCHAR),
        @Result(column = "TASK_NAME", property = "taskName", jdbcType = JdbcType.VARCHAR),
        @Result(column = "TASK_DESCRIPTION", property = "taskDescription", jdbcType = JdbcType.VARCHAR),
        @Result(column = "CLASS_NAME", property = "className", jdbcType = JdbcType.VARCHAR),
        @Result(column = "METHOD_NAME", property = "methodName", jdbcType = JdbcType.VARCHAR),
        @Result(column = "CRON", property = "cron", jdbcType = JdbcType.VARCHAR),
        @Result(column = "STAFFING", property = "staffing", jdbcType = JdbcType.VARCHAR),
        @Result(column = "NOTIFICATION_CONTENT", property = "notificationContent", jdbcType = JdbcType.VARCHAR),
        @Result(column = "NOTIFICATION_MODE", property = "notificationMode", jdbcType = JdbcType.VARCHAR),
        @Result(column = "STATE", property = "state", jdbcType = JdbcType.INTEGER),
        @Result(column = "CREATE_BY", property = "createBy", jdbcType = JdbcType.VARCHAR),
        @Result(column = "CREATE_TIME", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
        @Result(column = "UPDATE_BY", property = "updateBy", jdbcType = JdbcType.VARCHAR),
        @Result(column = "UPDATE_TIME", property = "updateTime", jdbcType = JdbcType.TIMESTAMP),
    })
    ScheduledTask selectByPrimaryKey(String id);

    /**
     * 插入定时任务信息对象.
     *
     * @param scheduledTask 定时任务信息
     */
    @Insert({
        "insert into AITK_SCHEDULED_TASK (ID, CLASSIFICATION, TASK_NAME, TASK_DESCRIPTION, CLASS_NAME, METHOD_NAME, ",
        "CRON, STAFFING, NOTIFICATION_CONTENT, NOTIFICATION_MODE, STATE, ",
        "CREATE_BY, CREATE_TIME, UPDATE_BY, UPDATE_TIME)",
        "values (#{id, jdbcType=VARCHAR}, #{classification, jdbcType=VARCHAR}, ",
        "#{taskName, jdbcType=VARCHAR}, #{taskDescription, jdbcType=VARCHAR}, ",
        "#{className, jdbcType=VARCHAR}, #{methodName, jdbcType=VARCHAR}, ",
        "#{cron, jdbcType=VARCHAR}, #{staffing, jdbcType=VARCHAR}, ",
        "#{notificationContent, jdbcType=VARCHAR}, #{notificationMode, jdbcType=VARCHAR}, ",
        "#{state, jdbcType=VARCHAR}, #{createBy, jdbcType=VARCHAR}, ",
        "#{createTime, jdbcType=TIMESTAMP}, #{updateBy, jdbcType=VARCHAR}, ",
        "#{updateTime, jdbcType=TIMESTAMP})"
    })
    void insertScheduledTask(final ScheduledTask scheduledTask);

}
